package leetCode;

import jianzhiOffer.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 113. 路径总和 II
 *
 * @author 司松坡
 * @createTime 2025年01月06日
 */
public class LeetCode113 {
    private List<List<Integer>> list = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<Integer> sonList = new ArrayList<>();
        solve(root, targetSum, 0, sonList);
        return list;
    }

    private void solve(TreeNode treeNode, int targetSum, int sum, List<Integer> sonList) {
        if (treeNode == null) {
            return;
        }
        sum += treeNode.val;
        sonList.add(treeNode.val);
        if (treeNode.left == null && treeNode.right == null) {
            if (sum == targetSum) {
                list.add(new ArrayList<>(sonList));
            }
        } else {
            solve(treeNode.left, targetSum, sum, sonList);
            solve(treeNode.right, targetSum, sum, sonList);
        }
        sonList.remove(sonList.size() - 1);
    }
}
